<?php

namespace Biz\service\advertisement;


use Biz\cache\advertisement\CNewUserProfile;
use Biz\cache\advertisement\CUserLabels;
use Biz\cache\advertisement\CUserProfile;
use Biz\model\advertisement\MHhzPositionUserprofile;
use Biz\model\advertisement\MTaskRecord;
use Biz\model\bigData\MUserProfileTags;
use Doraemon\service\position\BasePositionUserprofile;
use Doraemon\service\position\DisplayConstInterface;
use Doraemon\service\position\PositionRoute;
use Doraemon\service\position\Search\UserFilterSchedule;
use Doraemon\service\position\UserProfileModule\SystemType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\HhzEnv;
use Doraemon\tools\Log;
use Exception;

/**
 * @property mixed|null $uid 用户uid 实时 5204625
 * @property mixed|null $birthday 生日 实时 19831112
 * @property mixed|null $addtime 注册时间 实时 1539084175
 * @property mixed|null $app_version app版本 4.11.1
 */
class AdPositionManagement extends AdModel
{

    const CONF_PATH = '/data/wwwroot/g_biz/service/advertisement/static/position.php';
    protected static $config = [];

    public static function getConf()
    {
        if (empty(self::$config)) {
            self::$config = require_once self::CONF_PATH;
        }
        return self::$config;
    }

    public static function getConfById($id)
    {
        $conf = AdPositionManagement::getConf();
        return $conf[$id] ?? [];
    }

    public static function getNameById($id)
    {
        $conf = AdPositionManagement::getConf();
        return $conf[$id]['en_name'] ?? false;
    }

    /**
     * @throws Exception
     */
    public static function sort($sortInfo)
    {
        $conf = AdPositionManagement::getConf();
        $inputSort = [];
        //$sort 是 0 开始的序号,取出配置中的 sort 值,进行排序后替换
        foreach ($sortInfo as $id => $sort) {
            $inputSort[] = $conf[$id]['sort'] ?? $sort;
        }
        sort($inputSort);
        $realSort = [];
        foreach ($sortInfo as $id => $sort) {
            $realSort[$id] = array_shift($inputSort);
        }

        $new = [];
        foreach ($conf as $id => $v) {
            if (isset($sortInfo[$id])) {
                $v['sort'] = $realSort[$id];
            }
            $new[$id] = $v;
        }
        self::writeConf($new);
    }

    /**
     * @throws Exception
     */
    public static function createAppendPosition($id, $data)
    {
        $conf = AdPositionManagement::getConf();
        $fields = array_column($conf, null, 'en_name');
        $en_name = $data['en_name'] ?? '';
        $alias = $data['alias'] ?? [];
        if (isset($fields[$en_name]) && $fields[$en_name]['id'] != $id) throw new Exception('1.标识符或别名已经存在:' . $en_name);
        foreach ($alias as $aliasItem) {
            if (isset($fields[$aliasItem]) && $fields[$en_name]['id'] != $id) throw new Exception('2.标识符或别名已经存在:' . $aliasItem);
        }
        $old = $id ? $conf[$id] : [];
        $writeId = $id ?: max(array_keys($conf)) + 1;
        $sort = $id ? [] : ['sort' => count($conf), 'id' => $writeId];
        $writeData = $data + $old + $sort;
        ksort($writeData);
        $conf[$writeId] = $writeData;
        self::writeConf($conf);
    }

    /**
     * @throws Exception
     */
    public static function createPosition($en_name, $app_tab = '0', $style_type = '')
    {
        $conf = AdPositionManagement::getConf();
        $fields = array_column($conf, null, 'en_name');
        if (isset($fields[$en_name])) exit('已经存在:' . $en_name);
        $id = max(array_keys($conf)) + 1;
        $data = [
            'id' => (string)$id,
            'en_name' => $en_name,
            'style_type' => $style_type,
            'app_tab' => $app_tab,
            'rel' => '2',
        ];
        $conf[$id] = $data;
        self::writeConf($conf);
    }

    /**
     * @throws Exception
     */
    public static function writeConf($data)
    {
        return parent::setConf(self::CONF_PATH, $data);
    }

    /**
     * @throws Exception
     */
    public static function delPosition($id)
    {
        $conf = AdPositionManagement::getConf();
        if (!isset($conf[$id])) throw new Exception('配置不存在');
        unset($conf[$id]);
        self::writeConf($conf);
    }


}